declare namespace  xmldb="http://exist-db.org/xquery/xmldb";

declare function local:recipient_countries() as xs:string {
	let $countries:= (for $code in distinct-values(collection("/iAG/xml")/iati-activities/iati-activity/recipient-country/@code) 
	return 
		 if ($code!='') then
		 	subsequence(collection("/iAG/xml")/iati-activities/iati-activity/recipient-country[@code=$code],1,1)
		 else ()
	)
	let $result:=(<results>{for $country in $countries order by normalize-space($country/text())
	return
	   <item>
	       <label>{normalize-space($country/text())}</label><id>{data($country/@code)}</id>
	  </item>}
	</results>)
	let $exists := xmldb:collection-available("/iAG/cache"),
	$isLoggedIn := xmldb:login("xmldb:exist:///db", "exuser", "1at1")
	return
	if ( $exists ) then
	   xmldb:store("/iAG/cache","_recipient-country.xml",$result)
	else 
	  let $out := xmldb:create-collection("/iAG","cache")
	  return xmldb:store("/iAG/cache","_recipient-country.xml",$result)
};

declare function local:sectors() as xs:string {
	let $items:= (for $code in distinct-values(collection("/iAG/xml")/iati-activities/iati-activity/sector/@code) 
	return 
		if ($code!='') then
	    	subsequence(collection("/iAG/xml")/iati-activities/iati-activity/sector[@code=$code],1,1)
	    else ()
	)
	let $result:=(<results>{for $item in $items order by normalize-space($item/text())
	return
	   <item>
	       <label>{normalize-space($item/text())}</label><id>{data($item/@code)}</id>
	  </item>}
	</results>)
	let $exists := xmldb:collection-available("/iAG/cache"),
	$isLoggedIn := xmldb:login("xmldb:exist:///db", "exuser", "1at1")
	return
	if ( $exists ) then
	   xmldb:store("/iAG/cache","_sector.xml",$result)
	else 
	  let $out := xmldb:create-collection("/iAG","cache")
	  return xmldb:store("/iAG/cache","_sector.xml",$result)
};

declare function local:reporting_orgs() as xs:string {
	let $items:= (for $code in distinct-values(collection("/iAG/xml")/iati-activities/iati-activity/reporting-org/@ref) 
	return 
		if ($code!='') then
			subsequence(collection("/iAG/xml")/iati-activities/iati-activity/reporting-org[@ref=$code],1,1)
		else ()
	)
	let $result:=(<results>{for $item in $items order by normalize-space($item/text())
	return
	   <item>
	       <label>{normalize-space($item/text())}</label><id>{data($item/@ref)}</id>
	  </item>}
	</results>)
	let $exists := xmldb:collection-available("/iAG/cache"),
	$isLoggedIn := xmldb:login("xmldb:exist:///db", "exuser", "1at1")
	return
	if ( $exists ) then
	   xmldb:store("/iAG/cache","_reporting-org.xml",$result)
	else 
	  let $out := xmldb:create-collection("/iAG","cache")
	  return xmldb:store("/iAG/cache","_reporting-org.xml",$result)
};

declare function local:donors() as xs:string {
	let $items:= (for $code in distinct-values(collection("/iAG/xml")/iati-activities/iati-activity/participating-org[@role='Funding']/@ref) 
	return 
		if ($code!='') then
	    	subsequence(collection("/iAG/xml")/iati-activities/iati-activity/participating-org[@ref=$code],1,1)
	    else ()
	)
	let $result:=(<results>{for $item in $items order by normalize-space($item/text())
	return
	   <item>
	       <label>{normalize-space($item/text())}</label><id>{data($item/@ref)}</id>
	  </item>}
	</results>)
	let $exists := xmldb:collection-available("/iAG/cache"),
	$isLoggedIn := xmldb:login("xmldb:exist:///db", "exuser", "1at1")
	return
	if ( $exists ) then
	   xmldb:store("/iAG/cache","_participating-org.xml",$result)
	else 
	  let $out := xmldb:create-collection("/iAG","cache")
	  return xmldb:store("/iAG/cache","_participating-org.xml",$result)
};

local:recipient_countries(), local:sectors(), local:reporting_orgs(), local:donors()
